package mid

/*
* @author: hjz
* @time: 2025/6/5 21:45
* @desc:560. 和为 K 的子数组
 */
func subarraySum(nums []int, k int) int {
	sum := make([]int, len(nums)+1)
	sum[0] = nums[0]
	for i := 1; i < len(nums); i++ {
		sum[i] = sum[i-1] + nums[i]
	}
	m := make(map[int]int, len(sum)>>1)
	m[0] = 1
	var count int
	for i := range nums {
		j, ok := m[sum[i]-k]
		if ok {
			count += j
		}
		m[sum[i]]++
	}
	return count
}
